#ifndef __HDVB_CACHEDDB_H
#define __HDVB_CACHEDDB_H
/*
    libhdvbd - Library for building sophisticated DVB applications
    Copyright (C) 2012 Tony Houghton <h@realh.co.uk>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/* Interface for caching an sqlite3 database in RAM */

#include <glib.h>
#include <glib-object.h>

#include <hdvb/dbcommon.h>

G_BEGIN_DECLS

#define HDVB_TYPE_CACHED_DB \
        (hdvb_cached_db_get_type())
#define HDVB_CACHED_DB(obj) \
        (G_TYPE_CHECK_INSTANCE_CAST((obj), \
        HDVB_TYPE_CACHED_DB, HdvbCachedDb))
#define HDVB_IS_CACHED_DB(obj) \
        (G_TYPE_CHECK_INSTANCE_TYPE((obj), \
        HDVB_TYPE_CACHED_DB))
#define HDVB_CACHED_DB_CLASS(klass) \
        (G_TYPE_CHECK_CLASS_CAST((klass), \
        HDVB_TYPE_CACHED_DB, HdvbCachedDbClass))
#define HDVB_IS_CACHED_DB_CLASS(klass) \
        (G_TYPE_CHECK_CLASS_TYPE((klass), \
        HDVB_TYPE_CACHED_DB))
#define HDVB_CACHED_DB_GET_CLASS(obj) \
        (G_TYPE_INSTANCE_GET_CLASS((obj), \
        HDVB_TYPE_CACHED_DB, HdvbCachedDbClass))

typedef struct _HdvbCachedDb        HdvbCachedDb;
typedef struct _HdvbCachedDbClass   HdvbCachedDbClass;

/* Identical to HdvbDbCreator but saves an include */
typedef HdvbDbCreator HdvbCachedDbCreator;

struct _HdvbCachedDb {
    GObject parent_instance;
    char *filename;
    gboolean cache;
    gboolean create_schemas;
    sqlite3 *db;
    HdvbCachedDbCreator creator;
    gpointer creator_handle;
    int version;
};

struct _HdvbCachedDbClass {
    GObjectClass parent_class;
    /* Virtual with default implementations */
    gboolean (*db_open)(HdvbCachedDb *self);
    gboolean (*sync)(HdvbCachedDb *self);
    gboolean (*copy_data)(HdvbCachedDb *self, sqlite3 *src, sqlite3 *dest);
};

GType
hdvb_cached_db_get_type(void) G_GNUC_CONST;


/* If cache is FALSE the on-disc file is used directly.
 * create_schemas indicates whether the implementation of copy_data requires
 * tables etc to be created in the target database first before copying; the
 * default implementation doesn't require that. 
 */
HdvbCachedDb *
hdvb_cached_db_open(const char *filename,
        gboolean cache, gboolean create_schemas,
        HdvbCachedDbCreator creator,
        int version, gpointer creator_handle);

/* Commits the database to disc (noop if cache is FALSE) */
gboolean
hdvb_cached_db_sync(HdvbCachedDb *self);

G_END_DECLS

#endif /* __HDVB_CACHEDDB_H */
